package com.a3dweijing.wetouch2.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.BannerImageLoader2;
import com.a3dweijing.wetouch2.adapter.BusinessAdapter;
import com.a3dweijing.wetouch2.bean.BusinessMsg;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yxd on 2017/10/31.
 */

public class ThreeDBusinessFragment extends Fragment {
    private Handler handler;
    private Banner banner;
    private RecyclerView businessRec;
    private List<String> mListImage, mListTitle;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_threed_business,container,false);
        banner= (Banner) view.findViewById(R.id.banner);
        businessRec= (RecyclerView) view.findViewById(R.id.business_rec);
        businessRec.setLayoutManager(new LinearLayoutManager(getContext()));
        dataBuilding();
        bannerInit();
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 100:
                        BusinessMsg businessMsg=new Gson().fromJson(msg.obj.toString(),BusinessMsg.class);
                        if (businessMsg!=null && businessMsg.isFlag() && businessMsg.getDemandList()!=null && businessMsg.getDemandList().size()>0){
                            businessRec.setAdapter(new BusinessAdapter(businessMsg.getDemandList()));
                        }
                        break;
                    case 404:
                        break;
                }
            }
        };
        new GetBusinessThread("1").start();
        return view;
    }
    public static ThreeDBusinessFragment newinstance()
    {
        Bundle bundle=new Bundle();
        bundle.putString("name","商务合作");
        ThreeDBusinessFragment threeDBusinessFragment=new ThreeDBusinessFragment();
        threeDBusinessFragment.setArguments(bundle);
        return threeDBusinessFragment;
    }
    private class GetBusinessThread extends Thread {
        String pageNo;

        public GetBusinessThread(String pageNo) {
            this.pageNo = pageNo;
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetBusinessData(pageNo);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }


    }
    private void dataBuilding(){
        mListTitle = new ArrayList<>();
        //将标题放入集合
        mListTitle.add("第一张图片");
        mListTitle.add("第二张图片");
        mListTitle.add("第三张图片");
        mListTitle.add("第四张图片");
        mListTitle.add("第五张图片");

        mListImage = new ArrayList<>();
        //将图片放入集合中
        mListImage.add("http://img3.imgtn.bdimg.com/it/u=3823223463,3047520970&fm=23&gp=0.jpg");
        mListImage.add("http://img.hc360.com/auto-a/info/images/200803/8998419-10.jpg");
        mListImage.add("http://img.ichemo.cn/model/5188ebc96c74dd.jpg");
        mListImage.add("http://img0.imgtn.bdimg.com/it/u=2631448432,2817427988&fm=214&gp=0.jpg");
        mListImage.add("http://i.ebayimg.com/00/s/MzU1WDcwOQ==/%24%28KGrHqR,%21hYFDuguSMpNBQ8e+seDCQ~~60_1.JPG?set_id=880000500F");
    }
    private void bannerInit(){
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new BannerImageLoader2());
        //实例化图片集合
        //设置Banner图片集合
        banner.setImages(mListImage);
        //设置Banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //实例化Title集合

        //设置Banner标题集合（当banner样式有显示title时）
        banner.setBannerTitles(mListTitle);
        //设置轮播时间
        banner.setDelayTime(1000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //Banner设置方法全部调用完毕时最后调用
        banner.start();
    }

}
